package com.webserver.core;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @author 马强
 * @Description
 * @create 2022-04-12 10:33
 */
//用于请求处理
public class DispatcherServlet {
    private static File root;
    private static File staticDir;

    static {
        try {
            root = new File(ClientHandler.class.getClassLoader().getResource(".").toURI());
           staticDir = new File(root,"Static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

    }

    public void  service(HttpServletRequest request , HttpServletResponse response) throws IOException, URISyntaxException {
        //通过请求对象，获取浏览器地址栏中的抽象部分路径0
        String path =request.getUri();
        File file = new File(staticDir, path);
        System.out.println("资源是否存在："+file.exists());
        if(file.isFile()){
            response.setContentFile(file);
        }else {
            response.setStatusCode(404);
            response.setStatusReason("NotFound");
            file=new File(staticDir,"root/404.html");
            response.setContentFile(file);
        }

        ;


    }
}
